package com.acme.bankapp.domain.bank;

import com.acme.bankapp.exceptions.OverDraftLimitExceededException;

public class CheckingAccount extends AbstractAccount {

	private double overdraft;

	public CheckingAccount(double amount, double overdraft) {
		super(amount);
		if (overdraft < 0) {
			throw new IllegalArgumentException();
		}
		this.overdraft=overdraft;
	}
	
	@Override	
	public double maximumAmountToWithdraw() {
		return this.balance+this.overdraft; 
	}
	
	@Override
	public void withdraw(double amount) throws OverDraftLimitExceededException{
		if (maximumAmountToWithdraw() < amount) {
			throw new OverDraftLimitExceededException(" Over Draft Limit Exceeded. Amount=  "+amount+
					" maximumAmountToWithdraw =  "+maximumAmountToWithdraw());
		}
		if (this.balance > amount) {
			this.balance=this.balance-amount;
			System.out.println(" succsessful Your balance = "+this.balance+" your overdraft = "+this.overdraft);
			}
		else { 
			this.overdraft=maximumAmountToWithdraw()-amount;
			this.balance=0;
			System.out.println(" succsessful Your balance = "+this.balance+" your overdraft = "+this.overdraft);
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		long temp;
		temp = Double.doubleToLongBits(overdraft);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!super.equals(obj)) {
			return false;
		}
		if (!(obj instanceof CheckingAccount)) {
			return false;
		}
		CheckingAccount other = (CheckingAccount) obj;
		if (Double.doubleToLongBits(overdraft) != Double
				.doubleToLongBits(other.overdraft)) {
			return false;
		}
		return true;
	}
	
}
